package com.miao;


import com.miao.ForScan.People;
import com.miao.config.*;
import com.miao.entity.Babby;
import com.miao.entity.Boy;
import com.miao.entity.Cat;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.Arrays;


/**
* Description: 全部使用注解方式 注册Bean
* Author: Miao@ShaoQing
* Data: 9:36 2021/7/5
*/
public class MyTest02 {

    /**
    * Description:  配置类 @Configuration  +  @Import
    * Author: Miao@ShaoQing
    * Data: 10:54 2021/3/31
    */
    @Test
    public void test1() {

        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(JavaConfig1.class);
        //打印容器里面的全部Bean的名字
        Arrays.stream(applicationContext.getBeanDefinitionNames()).forEach(System.out::println);
        System.out.println("############### 以上是全部Bean ######################\n");

        //从容器里面取 Bean对象。
        Babby babby = (Babby) applicationContext.getBean("com.miao.entity.Babby");
        babby.setName("HeiHei");
        babby.setAge(2);
        System.out.println(babby);
    }


    /**
     * Description:  包扫描@component
     * Author: Miao@ShaoQing
     * Data: 10:55 2021/3/31
     */
    @Test
    public void test2() {

        ApplicationContext applicationContext = new AnnotationConfigApplicationContext("com.miao.ForScan");

        //打印容器里面的全部Bean的名字
        Arrays.stream(applicationContext.getBeanDefinitionNames()).forEach(System.out::println);
        System.out.println("############### 以上是全部Bean ######################\n");

        //从容器里面取 Bean对象。
        People people = (People) applicationContext.getBean("people");
        people.setName("LiLei");
        people.setAge(2);
        System.out.println(people);
    }



    /**
     * Description:  配置类 @Configuration  +  @Import + @Bean + @ComponentScan
     * Author: Miao@ShaoQing
     * Data: 10:54 2021/3/31
     */
    @Test
    public void test3() {

        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(JavaConfig2.class);

        //打印容器里面的全部Bean的名字
        Arrays.stream(applicationContext.getBeanDefinitionNames()).forEach(System.out::println);
        System.out.println("############### 以上是全部Bean ######################\n");

        //从容器里面取 Bean对象。
        Cat cat = (Cat) applicationContext.getBean("cat");
        cat.setName("HanMeiMei");
        cat.setAge(3);
        System.out.println(cat);
    }



    /**
     * Description: 展示@ComponentScan自定义过滤扫描 +  @Conditional条件检查 + ImportSelector条件选择
     * Author: Miao@ShaoQing
     * Data: 10:54 2021/3/31
     */
    @Test
    public void test4() {

        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(JavaConfig3.class);

        //打印容器里面的全部Bean的名字
        Arrays.stream(applicationContext.getBeanDefinitionNames()).forEach(System.out::println);
        System.out.println("############### 以上是全部Bean ######################\n");

        //从容器里面取出全部的Boy对象, 打印
        String[] names =  applicationContext.getBeanNamesForType(Boy.class);
        for(String name : names){
            System.out.println(name);
        }
    }


    /**
     * Description:  展示 ImportBeanDefinitionRegistrar 手动注册 Bean  + @ConditionalOnBean + @ConditionalOnProperty
     * Author: Miao@ShaoQing
     * Data: 10:54 2021/3/31
     */
    @Test
    public void test5()  {

        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(JavaConfig4.class);

        //打印容器里面的全部Bean的名字
        Arrays.stream(applicationContext.getBeanDefinitionNames()).forEach(System.out::println);
        System.out.println("############### 以上是全部Bean ######################\n");

    }


    /**
     * Description:  展示 FactoryBean 方式创建Bean
     * Author: Miao@ShaoQing
     * Data: 10:54 2021/3/31
     */
    @Test
    public void test6()  {

        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(JavaConfig5.class);

        //打印容器里面的全部Bean的名字
        Arrays.stream(applicationContext.getBeanDefinitionNames()).forEach(System.out::println);
        System.out.println("############### 以上是全部Bean ######################\n");


        //获取一下 FruitFactoryBean,
        Object bean1 = applicationContext.getBean("FruitFactoryBean");
        System.out.println("bean1 的类型：" + bean1.getClass());
        System.out.println("bean1 的内容："+ bean1.toString());


        //获取一下 &FruitFactoryBean,
        Object bean2 = applicationContext.getBean("&FruitFactoryBean");
        System.out.println("bean2 的类型：" + bean2.getClass());
        System.out.println("bean2 的内容："+ bean2.toString());
    }



}
